<?php

namespace api\modules\v1\controllers\member;

use yii\web\NotFoundHttpException;
use api\controllers\OnAuthController;
use common\enums\StatusEnum;
use common\helpers\ArrayHelper;
use common\models\bonus\BonusDayMember;
use common\models\member\Member;
use common\models\task\TaskList;
use GeTui\Client\Task;
use Yii;
/**
 * 会员接口
 *
 * Class MemberController
 * @package api\modules\v1\controllers\member
 * @property \yii\db\ActiveRecord $modelClass
 * @author  
 */
class MemberController extends OnAuthController
{
    /**
     * @var Member
     */
    public $modelClass = Member::class;

    /**
     * 单个显示
     *
     * @param $id
     * @return mixed
     * @throws NotFoundHttpException
     */
    public function actionView($id)
    {
        $model = $this->modelClass::find()
            ->where(['id' => $id, 'status' => StatusEnum::ENABLED])
            ->select([
                'id', 'username', 'nickname',
                'realname', 'head_portrait', 'gender',
                'qq', 'email', 'birthday',
                'status', 'created_at'
            ])
            ->asArray()
            ->one();

        if (!$model) {
            throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');
        }

        return $model;
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, ['delete', 'index'])) {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }

    public function actionInfo()
    {
        $member = Yii::$app->user->identity->member;
        $memberLevel = $member->memberLevel;
        $account = $member->account;
        //今日收入
        $today = strtotime(date('Y-m-d'));
        $today_bonus=BonusDayMember::find()->where(['member_id'=>$member->id, 'created_at'=>$today])->asArray()->select('bonus_all')->one();
        $day_earnings = 0;
        if($today_bonus){
            $day_earnings = $today_bonus['bonus_all'];
        }
        //今日任务
        $task_count = TaskList::find()->where(['member_id'=>$member->id, 'today_time'=>$today])->count();
        $today_task = $task_count.'/'.$memberLevel->daily_tasks;
        //昨日收入
        $yesterday = strtotime(date('Y-m-d') . ' -1 day');
        $yesterday_bonus = BonusDayMember::find()->where(['member_id' => $member->id, 'created_at' => $yesterday])->asArray()->select('bonus_all')->one();
        $yesterday_earnings = 0;
        if ($yesterday_bonus) {
            $yesterday_earnings = $yesterday_bonus['bonus_all'];
        }
        //本月收入
        $this_month_earnings = 0;
        $this_month_begin_date = strtotime(date('Y-m-01', strtotime(date("Y-m-d"))));
        $this_month_last_date = strtotime(date('Y-m-d', strtotime("$this_month_begin_date +1 month -1 day")));
        $this_month_bonus = BonusDayMember::find()->where(['member_id' => $member->id])->andWhere(['between', 'created_at',$this_month_begin_date,$this_month_last_date])->asArray()->sum('bonus_all');
        $this_month_earnings = 0;
        if ($this_month_bonus) {
            $this_month_earnings = $this_month_bonus;
        }
        //上月收入
        $last_month_earnings = 0;
        $last_month_begin_date = strtotime(date('Y-m-d', strtotime(date('Y-m-01') . ' -1 month')));
        $last_month_last_date = strtotime(date('Y-m-d', strtotime(date('Y-m-01') . ' -1 day')));
        $last_month_bonus = BonusDayMember::find()->where(['member_id' => $member->id])->andWhere(['between', 'created_at', $last_month_begin_date, $last_month_last_date])->asArray()->sum('bonus_all');
        $last_month_earnings = 0;
        if ($last_month_bonus) {
            $last_month_earnings = $this_month_bonus;
        }

        $list = [
            
                'bonus_amount' => $account->bonus_amount,
                'balance' => $account->user_money,
                'usdt' => $account->user_money,
                'day_earnings' => $day_earnings,
                'yesterday_earnings' => $yesterday_earnings,
                'this_month_earnings' => $this_month_earnings,
                'last_month_earnings' => $last_month_earnings,
                'today_task'=> $today_task
        ];
        $member = ArrayHelper::toArray($member);
        unset($member['password_hash'], $member['auth_key'], $member['password_reset_token']);
        $result['member'] = $member;
        $result['member']['account'] = ArrayHelper::toArray($account);
        $result['member']['memberLevel'] = ArrayHelper::toArray($memberLevel);
        $result['member']['bonus'] = $list;
        return $result;
    }
}
